﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using SIPO.Library;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.Enum;
using SIPO.Library.Interfaces;
using SIPO.Library.DomainObject.Administration.Catalog;

namespace SIPO.Library.DataAccess.Transaction
{
     public partial class recExpenseRepo
    {
         public object SelectScalar(SQL.Function.Aggregate function, string column, string parameters)
         {
             object _result = null;
             using (SqlConnection conn = new SqlConnection(connectionString))
             {
                 using (SqlCommand cmd = conn.CreateCommand())
                 {
                     StringBuilder sbQuery = new StringBuilder();
                     switch (function)
                     {
                         case SQL.Function.Aggregate.Max:
                             sbQuery.AppendFormat("SELECT MAX({0}) FROM transactions.recExpense {1}", column, parameters);
                             break;
                         case SQL.Function.Aggregate.Min:
                             sbQuery.AppendFormat("SELECT MIN({0}) FROM transactions.recExpense {1}", column, parameters);
                             break;
                         case SQL.Function.Aggregate.Distinct:
                             sbQuery.AppendFormat("SELECT DISTINCT({0}) FROM transactions.recExpense {1}", column, parameters);
                             break;
                         case SQL.Function.Aggregate.Count:
                             sbQuery.AppendFormat("SELECT COUNT({0}) FROM transactions.recExpense {1}", column, parameters);
                             break;
                         case SQL.Function.Aggregate.Sum:
                             sbQuery.AppendFormat("SELECT SUM({0}) FROM transactions.recExpense {1}", column, parameters);
                             break;
                         case SQL.Function.Aggregate.Avg:
                             sbQuery.AppendFormat("SELECT AVG({0}) FROM transactions.recExpense {1}", column, parameters);
                             break;
                         default:
                             // do nothing 
                             break;
                     }

                     cmd.CommandText = sbQuery.ToString();
                     cmd.CommandType = CommandType.Text;
                     try
                     {
                         conn.Open();
                         _result = cmd.ExecuteScalar();
                     }
                     catch (Exception ex)
                     {
                         throw (ex);
                     }
                     finally
                     {
                         if (conn.State == ConnectionState.Open) conn.Close();
                     }
                 }
             }
             return _result;
         }
         public List<recExpense> GetList(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
         {
             List<recExpense> tt = new List<recExpense>();
             using (SqlConnection conn = new SqlConnection(connectionString))
             {
                 using (SqlCommand cmd = conn.CreateCommand())
                 {
                     int startRow = ((currentPage - 1) * pageSize) + 1;
                     int endRow = (startRow + pageSize);

                     StringBuilder sbQuery = new StringBuilder();
                     sbQuery.AppendFormat(" WITH result_set AS ");
                     sbQuery.AppendFormat(" ( ");
                     sbQuery.AppendFormat("    SELECT ");
                     sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                     sbQuery.AppendFormat("      ExpenseID, TransDate, TransDesc, TransNo, TransAlocationID, TransAlocationName, TransValues, ReviewDate, IsActive,CreatedDate, CreatedBy, UpdatedDate, UpdatedBy, AttachFileName ");
                     sbQuery.AppendFormat("    FROM ");
                     sbQuery.AppendFormat("      transactions.recExpense ");
                     sbQuery.AppendFormat(" {0} ", parameters);
                     sbQuery.AppendFormat(" ) ");
                     sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                     cmd.CommandText = sbQuery.ToString();
                     cmd.CommandType = CommandType.Text;
                     try
                     {
                         conn.Open();
                         using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                         {
                             while (dr.Read())
                             {
                                 recExpense t = new recExpense();
                                 t.ExpenseID = dr[recExpense.Fields.ExpenseID] == DBNull.Value ? 0 : int.Parse(dr[recExpense.Fields.ExpenseID].ToString());
                                 t.TransDate = dr[recExpense.Fields.TransDate] == DBNull.Value ? DateTime.Now : DateTime.Parse(dr[recExpense.Fields.TransDate].ToString());
                                 t.TransDesc = dr[recExpense.Fields.TransDesc] == DBNull.Value ? string.Empty : dr[recExpense.Fields.TransDesc].ToString();
                                 t.TransNo = dr[recExpense.Fields.TransNo] == DBNull.Value ? string.Empty : dr[recExpense.Fields.TransNo].ToString();
                                 t.TransAlocationID = dr[recExpense.Fields.TransAlocationID] == DBNull.Value ? string.Empty : dr[recExpense.Fields.TransAlocationID].ToString();
                                 t.TransAlocationName = dr[recExpense.Fields.TransAlocationName] == DBNull.Value ? string.Empty : dr[recExpense.Fields.TransAlocationName].ToString();
                                 t.TransValues = dr[recExpense.Fields.TransValues] == DBNull.Value ? (decimal?)null : decimal.Parse(dr[recExpense.Fields.TransValues].ToString());
                                 t.ReviewDate = dr[recExpense.Fields.ReviewDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recExpense.Fields.ReviewDate].ToString());
                                 t.IsActive = dr[recExpense.Fields.IsActive] == DBNull.Value ? (bool?)null : bool.Parse(dr[recExpense.Fields.IsActive].ToString());
                                 t.CreatedDate = dr[recExpense.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recExpense.Fields.CreatedDate].ToString());
                                 t.CreatedBy = dr[recExpense.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recExpense.Fields.CreatedBy].ToString();
                                 t.UpdatedDate = dr[recExpense.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recExpense.Fields.UpdatedDate].ToString());
                                 t.UpdatedBy = dr[recExpense.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recExpense.Fields.UpdatedBy].ToString();
                                 t.AttachFileName = dr[recExpense.Fields.AttachFileName] == DBNull.Value ? string.Empty : dr[recExpense.Fields.AttachFileName].ToString();
                                 tt.Add(t);
                                 t.Dispose();
                             }
                         }
                     }
                     catch (Exception ex)
                     {
                         throw (ex);
                     }
                     finally
                     {
                         if (conn.State == ConnectionState.Open) conn.Close();
                     }
                 }
             }
             return tt;
         }

         public int GetMaxID()
         {
             SqlConnection conn = new SqlConnection(connectionString);
             SqlCommand cmd = new SqlCommand("select max(ExpenseID) from transactions.recExpense", conn);
             try {
                 conn.Open();
                 return (cmd.ExecuteScalar()==DBNull.Value? 1 : (int)cmd.ExecuteScalar()+ 1);
             }
             catch (Exception ex)
             {
                 throw (ex);
             }
                     finally
                     {
                         if (conn.State == ConnectionState.Open) conn.Close();
                     }
             
         }

         public object Review(string reviewdate, string ID )
         {
             SqlConnection conn = new SqlConnection(connectionString);
             SqlCommand cmd = new SqlCommand("update transactions.recExpense set ReviewDate= '" + reviewdate + "' where ExpenseID='" + ID + "'", conn);
             try
             {
                 conn.Open();
                 cmd.ExecuteNonQuery();
                 return true;
             }
             catch (Exception ex)
             {
                 throw (ex);
             }
             finally
             {
                 if (conn.State == ConnectionState.Open) conn.Close();
             }
             
         }

         public bool UpdateWithoutFile(recExpense domain)
         {
             int result = 0;
             using (SqlConnection conn = new SqlConnection(connectionString))
             {
                 using (SqlCommand cmd = conn.CreateCommand())
                 {
                     cmd.CommandText = "UPDATE transactions.recExpense SET ExpenseID = @ExpenseID, TransDate = @TransDate, TransDesc = @TransDesc, TransNo = @TransNo, TransAlocationID = @TransAlocationID, TransAlocationName = @TransAlocationName, TransValues = @TransValues, ReviewDate = @ReviewDate, IsActive = @IsActive, UpdatedDate = @UpdatedDate, UpdatedBy = @UpdatedBy WHERE ExpenseID = @ExpenseID";
                     cmd.CommandType = CommandType.Text;
                     cmd.Parameters.Add("@ExpenseID", SqlDbType.Int).Value = domain.ExpenseID;
                     cmd.Parameters.Add("@TransDate", SqlDbType.DateTime).Value = domain.TransDate;
                     cmd.Parameters.Add("@TransDesc", SqlDbType.VarChar).Value = (object)domain.TransDesc ?? DBNull.Value;
                     cmd.Parameters.Add("@TransNo", SqlDbType.VarChar).Value = (object)domain.TransNo ?? DBNull.Value;
                     cmd.Parameters.Add("@TransAlocationID", SqlDbType.VarChar).Value = (object)domain.TransAlocationID ?? DBNull.Value;
                     cmd.Parameters.Add("@TransAlocationName", SqlDbType.VarChar).Value = (object)domain.TransAlocationName ?? DBNull.Value;
                     cmd.Parameters.Add("@TransValues", SqlDbType.Decimal).Value = (object)domain.TransValues ?? DBNull.Value;
                     cmd.Parameters.Add("@ReviewDate", SqlDbType.DateTime).Value = (object)domain.ReviewDate ?? DBNull.Value;
                     cmd.Parameters.Add("@IsActive", SqlDbType.Bit).Value = (object)domain.IsActive ?? DBNull.Value;
                     cmd.Parameters.Add("@UpdatedDate", SqlDbType.DateTime).Value = (object)domain.UpdatedDate ?? DBNull.Value;
                     cmd.Parameters.Add("@UpdatedBy", SqlDbType.VarChar).Value = (object)domain.UpdatedBy ?? DBNull.Value;

                     try
                     {
                         conn.Open();
                         result = cmd.ExecuteNonQuery();
                     }
                     catch (Exception ex)
                     {
                         throw (ex);
                     }
                     finally
                     {
                         if (conn.State == ConnectionState.Open) conn.Close();
                     }
                 }
             }
             return (result > 0);
         }

         public List<recExpense> GetListReport(string parameter)
         {
             List<recExpense> tt = new List<recExpense>();
             using (SqlConnection conn = new SqlConnection(connectionString))
             {
                 using (SqlCommand cmd = conn.CreateCommand())
                 {
                     cmd.CommandText = "SELECT ExpenseID, TransDate, TransDesc, TransNo, TransAlocationID, TransAlocationName, TransValues, ReviewDate, IsActive, AttachFile, AttachFileName, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy FROM transactions.recExpense "+parameter;
                     cmd.CommandType = CommandType.Text;
                     try
                     {
                         conn.Open();
                         using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                         {
                             while (dr.Read())
                             {
                                 recExpense t = new recExpense();
                                 t.ExpenseID = dr[recExpense.Fields.ExpenseID] == DBNull.Value ? 0 : int.Parse(dr[recExpense.Fields.ExpenseID].ToString());
                                 t.TransDate = dr[recExpense.Fields.TransDate] == DBNull.Value ? DateTime.Now : DateTime.Parse(dr[recExpense.Fields.TransDate].ToString());
                                 t.TransDesc = dr[recExpense.Fields.TransDesc] == DBNull.Value ? string.Empty : dr[recExpense.Fields.TransDesc].ToString();
                                 t.TransNo = dr[recExpense.Fields.TransNo] == DBNull.Value ? string.Empty : dr[recExpense.Fields.TransNo].ToString();
                                 t.TransAlocationID = dr[recExpense.Fields.TransAlocationID] == DBNull.Value ? string.Empty : dr[recExpense.Fields.TransAlocationID].ToString();
                                 t.TransAlocationName = dr[recExpense.Fields.TransAlocationName] == DBNull.Value ? string.Empty : dr[recExpense.Fields.TransAlocationName].ToString();
                                 t.TransValues = dr[recExpense.Fields.TransValues] == DBNull.Value ? (decimal?)null : decimal.Parse(dr[recExpense.Fields.TransValues].ToString());
                                 t.ReviewDate = dr[recExpense.Fields.ReviewDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recExpense.Fields.ReviewDate].ToString());
                                 t.IsActive = dr[recExpense.Fields.IsActive] == DBNull.Value ? (bool?)null : bool.Parse(dr[recExpense.Fields.IsActive].ToString());
                                 t.AttachFile = dr[recExpense.Fields.AttachFile] == DBNull.Value ? null : (byte[])(dr[recExpense.Fields.AttachFile]);
                                 t.AttachFileName = dr[recExpense.Fields.AttachFileName] == DBNull.Value ? string.Empty : dr[recExpense.Fields.AttachFileName].ToString();
                                 t.CreatedDate = dr[recExpense.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recExpense.Fields.CreatedDate].ToString());
                                 t.CreatedBy = dr[recExpense.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recExpense.Fields.CreatedBy].ToString();
                                 t.UpdatedDate = dr[recExpense.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recExpense.Fields.UpdatedDate].ToString());
                                 t.UpdatedBy = dr[recExpense.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recExpense.Fields.UpdatedBy].ToString();
                                 tt.Add(t);
                                 t.Dispose();
                             }
                         }
                     }
                     catch (Exception ex)
                     {
                         throw (ex);
                     }
                     finally
                     {
                         if (conn.State == ConnectionState.Open) conn.Close();
                     }
                 }
             }
             return tt;
         }		

         public int ValidasiCode(string Code){
             SqlConnection conn = new SqlConnection(connectionString);
             SqlCommand cmd = new SqlCommand("select count(ExpenseID) from transactions.recExpense where lower(TransNo)='"+ Code.ToLower() +"'", conn);
             try
             {
                 conn.Open();
                 return ((int)cmd.ExecuteScalar());
             }
             catch (Exception ex)
             {
                 throw (ex);
             }
             finally
             {
                 if (conn.State == ConnectionState.Open) conn.Close();
             }
         }
   
     }
}
